<!DOCTYPE html>
<html lang="en">

<head>
    <#include "/lx/admin/components/meta.html" />
    <title>题目详情 ${metaTitleSuffix!}</title>
    <#include "/lx/admin/components/stylesheets.html" />
</head>

<body>
    <div id="app">
        <el-container>
            <#assign menuIndex="5-1" />
            <#include "/lx/admin/components/menu.html" />
            <el-container>
                <#include "/lx/admin/components/header.html" />
                <el-main>
                    <template>
                        <el-form ref="form" :model="info" label-width="100px">
                            <el-form-item label="题目">
                                <el-input v-model="info.question" placeholder="请输入题目"></el-input>
                            </el-form-item>
                            <el-form-item label="排序">
                                <el-input v-model="info.sort" type="number" placeholder="请输入排序"></el-input>
                            </el-form-item>
                            <el-form-item label="状态">
                                <el-radio-group v-model="info.status">
                                    <el-radio :label="1">正常</el-radio>
                                    <el-radio :label="0">禁用</el-radio>
                                </el-radio-group>
                            </el-form-item>
                            <el-form-item v-for="(option, index) in answers" :label="'选项 ' + option.no" class="option">
                                <el-input v-model="option.title" placeholder="请输入选项"></el-input>
                                <el-checkbox v-model="option.isAnswer">正确答案</el-checkbox>
                                <el-button type="danger" size="small" @click.prevent="removeOption(option)">删除</el-button>
                            </el-form-item>
                            <el-form-item>
                                <el-button type="success" size="small" @click="add"><i class="el-icon-plus"></i>添加选项</el-button>
                                <el-button type="primary" @click="submit" style="width: 120px">提交</el-button>
                            </el-form-item>
                        </el-form>
                    </template>
                </el-main>
            </el-container>
        </el-container>
        <#include "/lx/admin/components/footer.html" />
    </div>
    <#include "/lx/admin/components/scripts.html" />
    <script type="text/javascript">
    var vue = new Vue({
        el: '#app',
        data() {
            return {
                answerIsFilled: false,
                info: {},
                answers: [],
                courseId: '${courseId!}'
            }
        },
        watch: {

        },
        mounted() {
            this.info = ${info!'{}'}
            if (this.info.answers) {
                this.answers = JSON.parse(this.info.answers);
                for (var i = 0; i < this.answers.length; i ++) {
                    this.answers[i].isAnswer = this.answers[i].isAnswer == 1;
                }
            }
            this.setOpitionNo();
        },
        methods: {
            add() {
                console.log(this.info);
                this.answers.push({
                    no: "",
                    title: '',
                    isAnswer: false
                });
                this.setOpitionNo()
            },
            removeOption(option) {
                var index = this.answers.indexOf(option)
                if (index !== -1) {
                    this.answers.splice(index, 1)
                }
                this.setOpitionNo()
            },
            setOpitionNo() {
                const alphabet = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "N", "M", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
                for (let i = 0; i < this.answers.length; i++) {
                    this.answers[i].no = alphabet[i]
                }
            },
            submit() {
                var self = this;
                self.rightAnswerList = []
                for (let item of self.answers) {
                    if (item.isAnswer == true) {
                        self.rightAnswerList.push(item)
                    }
                    if (typeof item.title == "undefined" || item.title == null || item.title.match(/^[ ]*$/)) {
                        self.answerIsFilled = false
                    } else {
                        self.answerIsFilled = true
                    }
                }
                if (!self.info.question) {
                    msg("请输入题目");
                    return;
                }
                if (self.answerIsFilled == false) {
                    msg("请填满可选答案");
                    return;
                }
                if (self.rightAnswerList.length == 0) {
                    msg("请选择至少一个正确答案");
                    return;
                }

                // 答案列表
                var answers = new Array;
                for (var i = 0; i < self.answers.length; i ++) {
                    var answer = {
                        title: self.answers[i].title,
                        isAnswer: self.answers[i].isAnswer ? 1 : 0
                    }
                    answers.push(answer);
                }

                var url = "/lx/admin/cert/submitQuestion"
                var param = {
                    questionId: self.info.id,
                    courseId: self.courseId,
                    question: self.info.question,
                    answers: JSON.stringify(answers),
                    type: self.rightAnswerList.length == 1 ? 1 : 2,
                    sort: self.info.sort,
                    status: self.info.status
                };
                var success = () => {
                    if ('${referer!}' != '') {
                        window.location.href = '${referer!}';
                    } else {
                        window.location.href = "/lx/admin/cert/questionList";
                    }
                }
                var fail = (response) => {
                    msg(response.data.msg);
                }
                var complete = () => {}
                axiosRequestPro(url, param, success, fail, complete);
            }
        }
    })
    </script>
    <style type="text/css">
    .option .el-input {
        margin-right: 10px;
        width: 80%;
        vertical-align: top;
    }

    .option .el-checkbox {
        margin-right: 10px;
    }
    </style>
    <#include "/lx/admin/components/msg.html" />
</body>

</html>